<template>
  <el-container>
    <el-header>
      <img src="../assets/images/logo.png" alt="" />
      <el-menu
        mode="horizontal"
        background-color="#23262e"
        text-color="#fff"
        active-text-color="rgb(64,158,255)"
        unique-opened
      >
        <el-submenu index="2">
          <template slot="title">
            <img :src="user.user_pic" alt="" v-if="user.user_pic" class="avatar" />
            <img src="../assets/logo.png" alt="" class="avatar" v-else /> 个人中心
          </template>
          <el-menu-item index="2-1"><i class="el-icon-edit"></i>用户资料</el-menu-item>
          <el-menu-item index="2-2"><i class="el-icon-edit"></i>重置密码</el-menu-item>
          <el-menu-item index="2-3"><i class="el-icon-edit"></i>更换头像</el-menu-item>
        </el-submenu>
        <el-menu-item index="4-1" @click="logout">
          <i class="el-icon-switch-button"></i>退出</el-menu-item
        >
      </el-menu>
    </el-header>
    <el-container>
      <el-aside width="200px">
        <div class="user-info">
          <img class="avatar" :src="user.user_pic" alt="" v-if="user.user_pic" />
          <img class="avatar" src="../assets/logo.png" alt="" v-else />
          <p>
            欢迎你 <span>{{ user.nickname || user.username }}</span>
          </p>
        </div>
        <el-menu
          background-color="#23262e"
          text-color="#fff"
          active-text-color="rgb(64,158,255)"
          unique-opened
          router
        >
          <template v-for="item in menu">
            <!-- 循环创建没有children内容的情况 -->
            <el-menu-item
              :index="item.indexPath"
              v-if="item.children === null"
              :key="item.indexPath"
            >
              <i :class="item.icon"></i>{{ item.title }}
            </el-menu-item>
            <!-- 循环创建有children内容的情况 -->
            <el-submenu :index="item.indexPath" v-else :key="item.indexPath">
              <!-- 下拉菜单标题 -->
              <template slot="title"><i :class="item.icon"></i> {{ item.title }} </template>
              <!-- 循环创建下拉菜单内容 -->
              <template v-for="v in item.children">
                <el-menu-item :index="v.indexPath" :key="v.indexPath">
                  <i :class="v.icon"></i>{{ v.title }}
                </el-menu-item>
              </template>
              <!-- <el-menu-item index="2-2"><i class="el-icon-edit"></i>文章列表</el-menu-item> -->
            </el-submenu>
          </template>
          <!-- <el-submenu index="4">
            <template slot="title"><i class="el-icon-edit"></i>个人中心</template>
            <el-menu-item index="4-1"><i class="el-icon-edit"></i>重置密码</el-menu-item>
            <el-menu-item index="4-2"><i class="el-icon-edit"></i>用户资料</el-menu-item>
            <el-menu-item index="4-3"><i class="el-icon-edit"></i>更换头像</el-menu-item>
          </el-submenu> -->
        </el-menu>
      </el-aside>
      <el-main>
        <!-- 内容区放占位符，使用其他组件 -->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
import { mapState } from 'vuex'
import { getMenuAPI } from '@/api/menu.js'

export default {
  name: 'MyHome',
  data() {
    return {
      menu: []
    }
  },
  // 利用声明周期函数自动调用方法，调用 vuex 中的 actions 中的 getuser 方法
  created() {
    this.$store.dispatch('user/getUser')
    this.getMenu()
  },
  computed: {
    ...mapState('user', ['user'])
  },
  methods: {
    // 退出的方法
    logout() {
      this.$confirm('确定退出吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning' // 弹框的类型
      })
        .then(() => {
          // 点击了确定进入这里
          this.$store.commit('user/updateUser', {})
          this.$store.commit('user/updateToken', '')
          this.$router.push('/login')
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    // 获取菜单的数据
    async getMenu() {
      // 调用 API 方法，获取数据
      const { data: res } = await getMenuAPI()
      if (res.code === 0) {
        this.menu = res.data
      }
      // console.log(res)
    }
  }
}
</script>

<style lang="less" scoped>
.el-container {
  height: 100%;
  .avatar {
    height: 35px;
    width: 35px;
    border-radius: 50%;
    margin-right: 10px;
    // 下面的样式是，让图片按照原比例，完整的展示
    object-fit: cover;
    background-color: #fff;
  }
  .user-info {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 70px;
    p {
      color: #fff;
      font-size: 13px;
    }
  }
  // 头部
  .el-header {
    height: 60px;
    display: flex;
    justify-content: space-between;
    background-color: #23262e;
  }
  .el-container {
    // 侧边栏
    .el-aside {
      background-color: #23262e;
      height: 100%;
      .el-menu {
        width: 200px;
        overflow: hidden;
      }
    }
    // 内容
    .el-main {
      background-color: #f2f2f2;
    }
  }
}
</style>
